package com.bootcamp.javase.object;

import java.util.Objects;

/**
 * @program: ObjectCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-06 20:22
 **/

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        Star ss = new Star(12, "十二");
        Student sstu = new Student(44, "四十四", ss);
        Teacher tch = new Teacher(88, "八十八", sstu);
        Teacher tchClone = tch.clone();
        System.out.println("原来对象 student 和 star 地址：");
        System.out.println(tch.stu);
        System.out.println(tch.stu.s);
        System.out.println("克隆后对象 student 和 star 地址：");
        System.out.println(tchClone.stu);
        System.out.println(tchClone.stu.s);
//        tch.stu.s.name = "五十五";
        System.out.println(tchClone.stu.s.name);
        System.out.println(Objects.equals(tch.stu.s.name, tchClone.stu.s.name));
    }
}

class Teacher implements Cloneable{
    int age;
    String name;

    public Teacher(int age, String name, Student stu) {
        this.age = age;
        this.name = name;
        this.stu = stu;
    }

    Student stu;

    @Override
    protected Teacher clone() throws CloneNotSupportedException {
        Teacher tchClone = (Teacher)super.clone();
        tchClone.stu = this.stu.clone();
        return tchClone;
    }
}

class Student implements Cloneable{
    int age;

    public Student(int age, String name, Star s) {
        this.age = age;
        this.name = name;
        this.s = s;
    }

    String name;
    Star s;

    @Override
    protected Student clone() throws CloneNotSupportedException {
        Student stuClone = (Student)super.clone();
        stuClone.s = this.s.clone();
        return stuClone;
    }
}

class Star implements Cloneable{
    int age;
    String name;

    public Star(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    protected Star clone() throws CloneNotSupportedException {
        return (Star) super.clone();
    }
}
